package game;

import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.util.LinkedList;

public class ShootManager {
	
	//Variables
	private static ShootManager instance = null;
	private static LinkedList<Shoot> sList = new LinkedList<Shoot>();
	private static LinkedList<Shoot> shootEnemie = new LinkedList<Shoot>();
	public float time = 0.2f;
	private float timer;
	private int width = 15;
	private int height = 5;
	
	//Constructor
	private ShootManager() {}
	
	//Singleton
	public static ShootManager getInstance()
	{
		if (instance == null)
		{
			instance = new ShootManager();
		}
		
		return instance;
	}
	
	public void init()
	{
		sList.clear();
		shootEnemie.clear();
	}
	
	public boolean Fire(Point2D position, Point2D dimension, float rotation)
	{
		if (timer > time)
		{
			Audios.getInstance().Play(1);
			sList.add(new Shoot(4, new Point2D.Double(position.getX() + ((dimension.getX() / 2) - width / 2), 
													  position.getY() + ((dimension.getY() / 2) - height / 2)),
													  new Point2D.Double(width, height),
													  rotation,
													  25));
			timer = 0;
			return true;
		}
		return false;
	}
	
	public void FireEnemie(Point2D position, Point2D dimension, float rotation)
	{
		shootEnemie.add(new Shoot(10, new Point2D.Double(position.getX() + ((dimension.getX() / 2) - width / 2), 
				  position.getY() + ((dimension.getY() / 2) - height / 2)),
				  new Point2D.Double(width, height),
				  rotation,
				  5));
	}
	
	public void update()
	{
		timer += Game.loopTime;
		
		for (Shoot shoot : sList)
		{
			shoot.update();
			if (shoot.position.getX() > 800 || shoot.position.getX() < -shoot.dimension.getX() ||
				shoot.position.getY() > 600 || shoot.position.getY() < -shoot.dimension.getY())
			{
				sList.remove(shoot);
				break;
			}
		}
		
		for (Shoot shoot : shootEnemie)
		{
			shoot.update();
			if (shoot.position.getX() > (Game.screenWidth + 100) || shoot.position.getX() < -100 ||
				shoot.position.getY() > (Game.screenHeigth + 100) || shoot.position.getY() < -100)
			{
				shootEnemie.remove(shoot);
				break;
			}
		}
	}
	
	public boolean CollisionWithEnemy(Enemy enemy)
	{
		for (Shoot shoot : sList)
		{
			if (shoot.collision(enemy))
			{
				enemy.addHP(-shoot.dano);
				sList.remove(shoot);
				return true;
			}
		}
		return false;
	}
	
	public boolean CollisionWithPlayer(Player player)
	{
		for (Shoot shoot : shootEnemie)
		{
			if (shoot.collision(player))
			{
				player.addHP(-shoot.dano);
				shootEnemie.remove(shoot);
				return true;
			}
		}
		return false;
	}
	
	public void draw(Graphics g)
	{
		for (Shoot shoot : sList)
		{
			shoot.draw(g);
		}
		
		for (Shoot shoot : shootEnemie)
		{
			shoot.draw(g);
		}
	}
}
